package org.openstock.impl.def.core.util;

import java.awt.Component;
import java.awt.Font;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsEnvironment;
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.Toolkit;

import javax.swing.JOptionPane;

import org.openstock.impl.def.core.ui.swing.widget.MainFrame;

public class SwingUtil {

	public static Font mediumFont = new Font("Goudy Old Style",Font.PLAIN,14);
	private static Rectangle screenSize;
	
	public static void initializeWidget(Component component){
		component.setFont(mediumFont);
		/*component.setPreferredSize(
				new Dimension(component.getPreferredSize().width,mediumFont.getSize()+20));*/
	}
	

	
	public static Rectangle getScreenSize(){
		if(screenSize == null){
			 GraphicsConfiguration graphicsConfiguration = GraphicsEnvironment.
					 getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();
			Insets screenInsets = Toolkit.getDefaultToolkit().getScreenInsets(graphicsConfiguration);         
	        Rectangle screenSize = graphicsConfiguration.getBounds();
	        Rectangle maxBounds = new Rectangle(screenInsets.left + screenSize.x, 
	                screenInsets.top + screenSize.y, 
	                screenSize.x + screenSize.width - screenInsets.right - screenInsets.left,
	                screenSize.y + screenSize.height - screenInsets.bottom - screenInsets.top);
	        SwingUtil.screenSize = maxBounds;
		}
		return screenSize;
	}
	
	public static Rectangle getMaxAreaRectangle(Rectangle[] rectangles){
		Rectangle result = new Rectangle();
		if(rectangles != null && rectangles.length != 0){
			for(Rectangle rectangle : rectangles){
				if(result.width*result.height < rectangle.width*rectangle.height){
					result = rectangle;
				}
			}
		}
		return result;
	}
	

	
	public static void restrictPoint(java.awt.Point point,Rectangle rectangle){
		if(!rectangle.contains(point)){
			if(point.x < rectangle.x)
				point.x = rectangle.x;
			if(point.y < rectangle.y)
				point.y = rectangle.y;
			if(point.x > rectangle.x+rectangle.width)
				point.x = rectangle.x+rectangle.width;
			if(point.y > rectangle.y+rectangle.height)
				point.y = rectangle.y+rectangle.height;
		}
	}
	
	
	public static void restrictPointOutside(java.awt.Point point,Rectangle rectangle){
		if(!rectangle.contains(point)){
			if(point.x < rectangle.x)
				point.x = rectangle.x - 5;
			if(point.y < rectangle.y)
				point.y = rectangle.y - 5;
			if(point.x > rectangle.x+rectangle.width)
				point.x = rectangle.x+rectangle.width + 5;
			if(point.y > rectangle.y+rectangle.height)
				point.y = rectangle.y+rectangle.height + 5;
		}
	}
	
	
	public static void reportException(Throwable e){
		JOptionPane.showMessageDialog(MainFrame.getInstance(),
				getExceptionMessage(e),
			    "Error",
			    JOptionPane.ERROR_MESSAGE);
	}
	
	public static String getExceptionMessage(Throwable t){
		String message = t.getMessage();
		if(!StringUtil.hasText(message)){
			Throwable cause = t.getCause();
			if(cause != null){
				message = getExceptionMessage(cause);
			}else{
				StackTraceElement[] ste = t.getStackTrace();
				if(ste != null && ste.length > 0){
					StackTraceElement elem = ste[0];
					message = elem.getClassName()+" at "+Constant.lineSeparator
							+"File : "+elem.getFileName()+Constant.lineSeparator
							+"Class : "+elem.getClassName()+Constant.lineSeparator
							+"Method : "+elem.getMethodName()+Constant.lineSeparator
							+"Line : "+elem.getLineNumber()+Constant.lineSeparator;
				}
			}
		}
		return message;
	}

	
}
